var sectioning_scene = viewer.scene;
// 模型
let sectioning_tileset
// 剖面
let sectioning_clippingPlanes
// 模型中心点
let sectioning_center
// 实际剖面
let sectioning_plan
// 创建的点
let sectioning_creatPoint
// 起始点
let sectioning_startPoint
// 坐标数组
let sectioning_arrPoint = []
let mouseClickEvent = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let mouseMovementEvent = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let mouseLiftEvent = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let hander = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);






/**
 * 添加剖切
 * */
const HorizontalSection = () => {
    planeSlicingListeningEvent()
    sectioning_tileset = globalTilesetArr
    // viewer.scene.globe.depthTestAgainstTerrain = true
    hander.setInputAction(function(movement) {
        // 定义一个ClippingPlaneCollection类，用来存储裁剪面
        sectioning_clippingPlanes = new Cesium.ClippingPlaneCollection({
            planes : [
                // ClippingPlane对象数组集合
                new Cesium.ClippingPlane(new Cesium.Cartesian3(0.0, 0.0, -1.0),0.0), // 法线方向
            ],
            // edgeColor: Cesium.Color.RED,
            edgeWidth: 1.0, // 模型被裁切部分的截面线宽
            unionClippingRegions: true, //true 才能多个切割
        });
        let positionOne = getLocation(movement.position);
        if (positionOne === undefined || positionOne === false) {
            return
        }
        sectioning_startPoint = positionOne
        let positionTwo = {x:positionOne.x, y: positionOne.y, z: positionOne.z + 20}
        let  pointTwo = returnHeightPositionsectioning(positionOne, positionTwo)
        sectioning_arrPoint.push(positionOne,pointTwo)
        for (let globalTilesetArrKey of sectioning_tileset) {
            globalTilesetArrKey.clippingPlanes = sectioning_clippingPlanes;
        }
        // sectioning_tileset.clippingPlanes = sectioning_clippingPlanes;
        // var boundingSphere = tileset.boundingSphere;
        // 模型盒子边长
        // var radius = boundingSphere.radius;
        // 计算剖面框中心位置
        // sectioning_center = sectioning_tileset.boundingSphere.center.clone();
        const cartCenter = Cesium.Cartographic.fromCartesian(positionOne);
        for (var i = 0; i < sectioning_clippingPlanes.length; ++i) {
            var plane = sectioning_clippingPlanes.get(i);
            plane.distance = cartCenter.height
            sectioning_plan = plane
            viewer.entities.add({
                position: positionOne,
                direction: 'planeCuttingSrrow',
                id:'planeCuttingSrrow',
                name: 'planeCuttingSrrow',
                polyline: {
                    positions: new Cesium.CallbackProperty(()=>{
                        return sectioning_arrPoint
                    }, false),
                    width: 20,
                    // 带箭头的线
                    material: new Cesium.PolylineArrowMaterialProperty(
                        Cesium.Color.BLUE
                    ),
                },
            });
        }
        hander.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
// 鼠标点下事件

const planeSlicingListeningEvent = () => {
    mouseClickEvent.setInputAction((movement) => {
        let pickedObject = sectioning_scene.pick(movement.position);
        if (Cesium.defined(pickedObject) &&
            Cesium.defined(pickedObject.id) &&
            Cesium.defined(pickedObject.id.polyline)) {
            sectioning_creatPoint = pickedObject.id
            sectioning_creatPoint.polyline.material = new Cesium.PolylineArrowMaterialProperty(
                Cesium.Color.YELLOW
            )
            sectioning_scene.screenSpaceCameraController.enableInputs = false;
        }
    },Cesium.ScreenSpaceEventType.LEFT_DOWN)
// 鼠标抬起事件
    mouseLiftEvent.setInputAction((movement) => {
        if (Cesium.defined(sectioning_creatPoint)) {
            sectioning_creatPoint.polyline.material = new Cesium.PolylineArrowMaterialProperty(
                Cesium.Color.BLUE
            )
            sectioning_creatPoint = null
            sectioning_scene.screenSpaceCameraController.enableInputs = true;
        }
    },Cesium.ScreenSpaceEventType.LEFT_UP)
// 鼠标移动事件
    mouseMovementEvent.setInputAction((movement) => {
        if (Cesium.defined(sectioning_creatPoint)) {
            let num = movement.startPosition.y - movement.endPosition.y
            let positionOne = {x:sectioning_startPoint.x, y: sectioning_startPoint.y, z: sectioning_startPoint.z + num /10}
            let pointOne = returnHeightPositionsectioning(sectioning_startPoint, positionOne)
            let positionTwo = {x:pointOne.x, y: pointOne.y, z: pointOne.z + 20}
            let pointTwo = returnHeightPositionsectioning(pointOne, positionTwo)
            const cartCenter = Cesium.Cartographic.fromCartesian(pointOne);
            sectioning_startPoint = pointOne
            sectioning_arrPoint[0] = pointOne
            sectioning_arrPoint[1] = pointTwo
            sectioning_plan.distance = cartCenter.height

        }
    },Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}

/**
 * 名称：返回两点的高度坐标
 * 说明：通过传入的两点返回两点之间的高度坐标
 * 参数：positionsone：起点, positionstwo：终点
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function returnHeightPositionsectioning(positionsone, positionstwo){
    let point1cartographic = Cesium.Cartographic.fromCartesian(positionsone);
    let point2cartographic = Cesium.Cartographic.fromCartesian(positionstwo);
    let point_temp = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(point1cartographic.longitude),Cesium.Math.toDegrees(point1cartographic.latitude),point2cartographic.height);
    return point_temp
}


/**
 * 退出剖切
 * */
const exitCutting = () => {
    // 退出鼠标监听
    mouseMovementEvent.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    mouseLiftEvent.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
    mouseClickEvent.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN)
    hander.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)

    // 将剖面设置为空
    for (let globalTilesetArrKey of sectioning_tileset) {
        globalTilesetArrKey.clippingPlanes = new Cesium.ClippingPlaneCollection()
    }
    // 删除箭头
    viewer.entities.removeById('planeCuttingSrrow')
    sectioning_arrPoint = []

}
